﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using RM2012Service.DataAccess;
using RM2012Service.Business;
using System.Data;
using System.Data;

namespace RM2012Service.Controller
{
    public class CustomerCtrl
    {
        CustomerDAO dao;

        public CustomerCtrl()
        {
            dao = new CustomerDAO();
        }

        public DataSet GetAllCustomer()
        {
            return dao.GetAllCustomer();
        }

        public DataSet GetCustomerLastDataSet()
        {
            return dao.GetCustomerLastDataSet();
        }

        public CustomerInfo GetCustomerLast()
        {
            CustomerInfo info = null;
            try
            {
                DataTable dt = dao.GetCustomerLast();
                if (dt.Rows.Count > 0)
                {
                    info = new CustomerInfo();
                    info.CusId = dt.Rows[0]["CusId"].ToString();
                    info.FullName = dt.Rows[0]["FullName"].ToString();
                    bool sex = Convert.ToBoolean(dt.Rows[0]["Sex"].ToString());
                    info.Sex = sex;
                    double discountPer = Convert.ToDouble(dt.Rows[0]["DiscountPer"].ToString());
                    info.DiscountPer = discountPer;
                    double debt = Convert.ToDouble(dt.Rows[0]["Debt"].ToString());
                    info.Debt = debt;
                    info.Address = dt.Rows[0]["Address"].ToString();
                    info.Phone = dt.Rows[0]["Phone"].ToString();
                    info.Email = dt.Rows[0]["Email"].ToString();
                    info.State = dt.Rows[0]["State"].ToString();
                    info.Remark = dt.Rows[0]["Remark"].ToString();
                }
            }
            catch
            {
                info = null;
            }
            return info;
        }

        public bool Add(CustomerInfo info)
        {
            if (dao.Add(info) > 0)
            {
                return true;
            } 
            else
            {
                return false;
            }
        }

        public bool Update(CustomerInfo info)
        {
            if (dao.Update(info) > 0)
            {
                return true;
            } 
            else
            {
                return false;
            }
        }

        public bool UpdateSex(String cusId)
        {
            if (dao.UpdateSex(cusId) > 0)
            {
                return true;
            } 
            else
            {
                return false;
            }
        }
    }
}
